using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using YYJ.Utilities;
using System.Data;

namespace YYJ.Entities {
    class Attachment {
        public int ID {
            get { return _id; }
            set { _id = value; }
        }
        private int _id = Constant.DATA_ENTITY_INVALID_ID;

        public byte[] Buffer {
            get {
                if( _buffer == null ) {
                    string query = "Select Top 1 FileContent From Attachment Where ID=@id And IsDeleted=0";
                    DataTable dt = DataAccess.executeQuery( query, "i@id", this.ID );
                    if( dt.Rows.Count == 0 ) {
                        throw new Exception( "Failed to get file content of Attachment object by id : " + this.ID.ToString() + "." );
                    }
                    this.Buffer = ( byte[] ) dt.Rows[ 0 ][ 0 ];

                }
                return _buffer; 
            }
            set { _buffer = value; }
        }
        private byte[] _buffer;

        public string Name {
            get { return _name; }
            set { _name = value; }
        }
        private string _name;

        public Attachment( string path ) {
            this.Buffer = File.ReadAllBytes( path );
            
            // X://folder/folder/file.ext => file.ext
            string[] arr = path.Split( '\\' );
            _name = arr[ arr.Length - 1 ];
        }

        public Attachment() {

        }
        public Attachment( int id ) {
            string query = "Select Top 1 ID, FileName From Attachment Where ID=@id And IsDeleted=0";
            DataTable dt = DataAccess.executeQuery( query, "i@id", id );
            if( dt.Rows.Count == 0 ) {
                throw new Exception( "Failed to construct Attachment object by id : " + id.ToString() + "." );
            }
            this.ID = Convert.ToInt32( dt.Rows[ 0 ][ "ID" ] );
            this.Name = dt.Rows[ 0 ][ "FileName" ].ToString();
        }

        public void saveToFile( string path ) {
            File.WriteAllBytes( path, this.Buffer );
        }

        public void save() {
            if( this._id == Constant.DATA_ENTITY_INVALID_ID ) {
                string sql = "Insert Into Attachment Values ( @name, @content, 0 )";
                Dictionary<string, object> pms = new Dictionary<string, object>();
                pms.Add( "s@name", this.Name );
                pms.Add( "vb@content", this.Buffer );
                this.ID = Convert.ToInt32( DataAccess.executeNonQueryIdent( sql, pms ) );
            } else {
                if( this._buffer == null ) { // neither loaded nor changed yet, no need to save
                    return;
                }
                string sql = "Update Attachment Set FileName=@name, FileContent=@content Where ID=@id";
                Dictionary<string, object> pms = new Dictionary<string, object>();
                pms.Add( "s@name", this.Name );
                pms.Add( "vb@content", this.Buffer );
                pms.Add( "i@id", this.ID );
                DataAccess.executeNonQuery( sql, pms );
            }
        }

        public void remove() {
            string sql = "Update Attachment Set IsDeleted=1 Where ID=@id";
            DataAccess.executeNonQuery( sql, "i@id", this.ID );
        }
    }
}
